<?php


namespace App\Controller\App;


use App\Base\BaseController4App;
use App\Entity\User;
use App\Repository\BannerRepository;
use App\Repository\SalesCutRepository;
use App\Repository\SalesUserJuanRepository;
use App\Repository\SiteOilCategoryRepository;
use App\Repository\SiteRepository;
use App\Repository\UserRepository;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/app/sales")
 */
class AppSalesController extends BaseController4App
{
    /**
     * @Route("/my", name="AppSalesMyJuan")
     */
    public function AppSalesMyJuan(
        SalesUserJuanRepository $salesUserJuanRepository
    )
    {
        $user = $this->needToBeUser();
        $req = $this->getRequestData();
        $page = $req["page"]??1;

        $type = $req["type"]??1;//1:可用，2=使用，3=过期
        $cond["user"]=$user;
        $dateTime = new \DateTime();
        switch ($type) {
            case 1:
                $cond["status"]=1;
                $cond["expireAt"]['$gt'] = $dateTime;
                break;
            case 2:
                $cond["status"]=2;
                break;
            case 3:
                $cond["status"]['$in']=[1,3];
                $cond["expireAt"]['$lt'] = new \DateTime();
                break;
            default:
                return $this->json_out(1,"error",[]);
        }
        $data = $salesUserJuanRepository->findBy(
            $cond,
            array("id"=>"DESC"),
            10,
            $page * 10 - 10
        );
        $this->setGroups("user","app","juan");
        return $this->json_out(1,"OK",$data);
    }

    /**
     * @Route("/available", name="AppSalesMyJuanCanUse")
     */
    public function AppSalesMyJuanCanUse(
        SalesUserJuanRepository $salesUserJuanRepository
    )
    {
        $user = $this->needToBeUser();
        $req = $this->getRequestData();
        $money = $req["money"]??0;
        $cond["user"] = $user;
        $cond["status"]=1;
        $cond["expireAt"]['$gt'] = new \DateTime();
        $data = $salesUserJuanRepository->findBy(
            $cond,
            array("id"=>"DESC")
        );
        foreach ($data as $key => $row) {
            if ($money < $row->getJuan()->getPrice()) {
                unset($data[$key]);
            }
        }
        $this->setGroups("user","app","juan");
        return $this->json_out(1,"OK",array_values($data));
    }

}